﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System8ToyMC;

namespace t_System8ToyMCControls
{
    [TestClass]
    public class t_HistogramListDisplayUC : PresentationTest
    {
        HistogramListDisplayUC _val;
        ListBox _list;

        bool _isloaded = false;

        [TestInitialize]
        public void TestSetup()
        {
            _val = new HistogramListDisplayUC();
            _list = _val.FindName("HList") as ListBox;

            _isloaded = false;
            _val.Loaded += (o, a) => _isloaded = true;

            TestPanel.Children.Add(_val);
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(100)]
        public void TestCtor()
        {
            Assert.IsNotNull(_val, "Could not create the thing itself!");
            Assert.IsNotNull(_list, "Could not find the list!");
            EnqueueConditional(() => _isloaded);
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(2000)]
        public void TestAddAHistogram()
        {
            Histogram hf = new Histogram();
            bool loaded = false;
            hf.Loaded += (o, a) => loaded = true;
            EnqueueCallback(() => _val.AddPlot(hf));
            EnqueueConditional(() => loaded);
            EnqueueCallback(() => Assert.AreEqual(1, _list.Items.Count, "Incorrect number of items on the internal list"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void AddLotsOfPlots()
        {
            int count = 0;
            for (int i = 0; i < 10; i++)
            {
                EnqueueCallback(() =>
                {
                    Histogram hf = new Histogram() { Width = 800, Height = 600, XAxisName = "n.Total" };
                    hf.Loaded += (o, a) => count++;
                    _val.AddPlot(hf);
                });
            }
            EnqueueConditional(() => count >= 10);
            EnqueueCallback(() => Assert.AreEqual(10, count, "Wrong number of talks inserted?"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void TestFindPlot()
        {
            Histogram hf = new Histogram();
            bool loaded = false;
            hf.Loaded += (o, a) => loaded = true;
            EnqueueCallback(() => _val.AddPlot(hf));
            EnqueueConditional(() => loaded);
            EnqueueCallback(() => _val.RemovePlot(hf));
            EnqueueCallback(() => Assert.AreEqual(0, _list.Items.Count, "not empty list!"));
            EnqueueTestComplete();
        }
    }
}